<template>
  <div class="course-content-list">
    <!-- 下拉刷新组件 -->
    <van-pull-refresh v-model="isRefreshing" @refresh="onRefresh">
      <!-- 课程列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad">
        <van-cell
          v-for="item in list"
          :key="item.id"
          @click="$router.push({
            name: 'course-info',
            params: {
              courseId: item.id
            }
          })">
          <div>
            <!-- 所有课程与已购课程，图片数据属性名不同，检测后使用 -->
            <img :src="item.courseImgUrl || item.image">
          </div>
          <div class="course-info">
            <h3 v-text="item.courseName || item.name"></h3>
            <p v-html="item.previewFirstField" class="course-preview"></p>
            <p
              v-if="item.price"
              class="price-container">
              <span class="course-discount">{{item.discounts}}</span>
              <s class="course-price">￥{{item.price}}</s>
            </p>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
// import { getQueryCourses } from '@/services/course'
export default {
  name: 'CourseContentList',
  props: {
    fetchData: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      // 数据页数
      currentPage: 1,
      // 下拉刷新状态
      isRefreshing: false
    }
  },
  methods: {
    async onLoad () {
      const { data } = await this.fetchData({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })
      console.log(data)
      // 判断如果不存在数据，结束，否则，保存
      if (data.content && data.content.records && data.content.records.length !== 0) {
        this.list.push(...data.content.records)
      } else if (data.content && data.content.length !== 0) {
        console.log(data.content.length)
        this.list.push(...data.content)
      }
      // 下一次请求页
      this.currentPage++
      // 加载状态结束
      this.loading = false
      // 数据全部加载完成
      if (data.content && data.content.records && data.content.records.length < 10) {
        this.finished = true
      } else if (data.content && data.content.length < 10) {
        this.finished = true
      }
    },
    async onRefresh () {
      // 刷新列表，要先把当前页置为 1
      this.currentPage = 1
      const { data } = await this.fetchData({
        currentPage: this.currentPage,
        pageSize: 10,
        status: 1
      })
      // 判断如果不存在数据，结束，否则，保存
      if (data.content && data.content.records && data.content.records.length !== 0) {
        this.list = data.content.records
      } else if (data.content && data.content.length !== 0) {
        this.list = data.content
      }
      // 提示刷新成功
      this.$toast('刷新成功')
      // 刷新状态结束
      this.isRefreshing = false
    }
  }
}

</script>
<style lang='scss' scoped>
// 课程列表样式
.course-content-list {
  position: fixed;
  // 将 top 和 bottom 值的设置交由父组件设置
  right: 0;
  left: 0;
  top: 0;
  bottom: 0;
  overflow-y: auto
}

// 课程条目样式
.van-cell__value {
  display: flex;
  height: 100px;
  padding: 10px 0;
}
// 设置左边图片样式
.van-cell__value img {
  height: 100%;
  width: 75px;
  border-radius: 5px;
}
// 设置右边描述样式
.course-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 10px;
}
.course-info .course-preview {
  flex-grow: 1;
}
.course-info .course-discount {
  color: #ff7452;
  margin-right: 10px;
}
p, h3 {
  margin: 0;
}
</style>
